package com.base.cn.platform.os.controller.liveByLink;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.TT.TTServiceUtils;
import com.base.cn.platform.os.common.utils.ke96.Ke96ServiceUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.material.CouMaterial;
import com.base.cn.platform.os.entity.course.open.CourseOpen;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.entity.user.user.UserCondition;
import com.base.cn.platform.os.service.course.material.CouMaterialService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


@Controller
@RequestMapping("/admin/toLiveByLink")
public class AdminToLiveByLinkController extends AdminBaseController {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private CouMaterialService couMaterialService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private TTServiceUtils ttServiceUtils;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private CusUserService cusUserService;

    /**
     * 视频链接详情
     *
     * @param
     * @return
     */
    @RequestMapping("/toLiveByLinkByCC")
    public String toLiveByLinkByCC(HttpServletRequest request,
                               @RequestParam("playurl") String playurl,
                               @RequestParam("admincode") String admincode,
                               @RequestParam("ptcode") String ptcode,
                               @RequestParam("teachercode") String teachercode) {
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        request.setAttribute("liveId", vlasProviderMap.get("ccAppId"));
        request.setAttribute("playurl", playurl);
        request.setAttribute("admincode", admincode);
        request.setAttribute("ptcode", ptcode);
        request.setAttribute("teachercode", teachercode);
        return resultPage("/course/cloud/cclive/live-link");
    }

    /**视频链接详情
     * @param roomId
     * @return
     */
    @RequestMapping("/toLiveByLinkByTT")
    public String toLiveByLinkByTT(@RequestParam("roomId") BigDecimal roomId, HttpServletRequest request) {
        CourseOpen courseOpen = courseOpenService.findCourseOpenBySql("roomId = " + roomId);
        CouMaterial couMaterial = couMaterialService.findCouMaterialBySql("roomId = " + roomId);
        String type = ObjectUtils.isEmpty(courseOpen) ? "couMaterial" : "courseOpen";
        String id = "";
        if(ObjectUtils.isNotEmpty(courseOpen)){
            id = courseOpen.getId().toString();
        }
        if(ObjectUtils.isNotEmpty(couMaterial)){
            id = couMaterial.getId().toString();
        }
        request.setAttribute("type", type);
        request.setAttribute("id", id);
        return resultPage("/course/cloud/ttlive/live-link");
    }

    /**
     * 通过链接进入视频
     * @param id
     * @param type
     * @param liveType
     * @param name
     * @return
     * @throws Exception
     */
    @RequestMapping("/getLiveParam")
    @ResponseBody
    public Map<String, Object> getLiveParam(@RequestParam(value = "id") BigDecimal id,
                                            @RequestParam("type") String type,
                                            @RequestParam("liveType") String liveType,
                                            @RequestParam(value = "name", required = false) String name) throws Exception {
        Map param = new HashMap();
        param.put("role", type);
        if ("couMaterial".equals(liveType)) {
            Map<String, Object> materialMap = couMaterialService.findMaterialById(id, false, true);
            param.put("classId", materialMap.get("roomId"));
            if ("1".equals(type)) {
                param.put("classId", materialMap.get("roomId"));
                Map teacherMap = (Map) materialMap.get("teacher");
                param.put("nickName", teacherMap.get("teacherName"));
                param.put("uuid", teacherMap.get("id").toString());
                Map imageMap = (Map) teacherMap.get("imageMap");
                Map pcUrlMap = (Map) imageMap.get("pcUrlMap");
                param.put("avatar", pcUrlMap.get("large"));
            } else if ("2".equals(type)) {
                if (StringUtils.isEmpty(materialMap.get("assistantId")) || "0".equals(materialMap.get("assistantId").toString())) {
                    return ResultUtil.ERROR("该直播间未设置助教老师");
                } else {
                    Map<String, Object> teacherMap = sysTeacherService.findSysTeacherById(new BigDecimal(materialMap.get("assistantId").toString()), false, false, false, false, false);
                    param.put("nickName", teacherMap.get("teacherName"));
                    param.put("uuid", teacherMap.get("id").toString());
                    Map imageMap = (Map) teacherMap.get("imageMap");
                    Map pcUrlMap = (Map) imageMap.get("pcUrlMap");
                    param.put("avatar", pcUrlMap.get("large"));
                }
            }
        } else {
            //免费直播
            Map<String, Object> courseOpenmMap = courseOpenService.findCourseOpenById(id, false, true, false);
            param.put("classId", courseOpenmMap.get("roomId"));
            if ("1".equals(type)) {  //老师
                Map teacherMap = (Map) courseOpenmMap.get("teacher");
                param.put("uuid", teacherMap.get("id").toString());
                param.put("nickName", teacherMap.get("teacherName"));
                Map imageMap = (Map) teacherMap.get("imageMap");
                Map pcUrlMap = (Map) imageMap.get("pcUrlMap");
                param.put("avatar", pcUrlMap.get("large"));
            } else if ("2".equals(type)) { //助教
                if (StringUtils.isEmpty(courseOpenmMap.get("tutorOne")) || "0".equals(courseOpenmMap.get("tutorOne").toString())) {
                    return ResultUtil.ERROR("该直播间未设置助教老师");
                } else {
                    Map<String, Object> teacherMap = sysTeacherService.findSysTeacherById(new BigDecimal(courseOpenmMap.get("tutorOne").toString()), false, false, false, false, false);
                    param.put("nickName", teacherMap.get("teacherName"));
                    param.put("uuid", teacherMap.get("id").toString());
                    Map imageMap = (Map) teacherMap.get("imageMap");
                    Map pcUrlMap = (Map) imageMap.get("pcUrlMap");
                    param.put("avatar", pcUrlMap.get("large"));
                }
            }
        }
        if ("3".equals(type)) { //学生
            UserCondition user = new UserCondition();
            user.setMobile(name);
            PageInfo<CusUser> cusUserList = cusUserService.findUserListPage(user, 1, 1);
            if (cusUserList.getList().size() == 1) {
                CusUser cusUser = (CusUser) cusUserList.getList().get(0);
                param.put("uuid", cusUser.getId().toString());
                param.put("nickName", cusUser.getNickname());
                param.put("avatar", cusUser.getAvatar());
            } else {
                return ResultUtil.ERROR("没有该学生", null);
            }
        }
        return ttServiceUtils.getLiveParam(param);
    }

    @RequestMapping("/toLiveIframe")
    public String toLiveIframe(@RequestParam("url") String url, HttpServletRequest request) {
        request.setAttribute("url", url);
        return resultPage("/course/live/iframe-live");
    }
}
